//
//  ContactModelCell.m
//  YZZX
//
//  Created by Easaa on 15/5/6.
//  Copyright (c) 2015年 Easaa. All rights reserved.
//

#import "ContactModelCell.h"
#import "ContractView.h"

@interface ContactModelCell ()
@property (nonatomic,copy) void(^tapBlock)(NSDictionary *);


@end

@implementation ContactModelCell


-(id)initWithDataStyle:(UITableViewCellStyle)stylessss reuseIdentifier:(NSString *)nssstr
{
    self = [super initWithStyle:stylessss reuseIdentifier:nssstr];
    if(self)
    {
    
       
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
    
    }

    return self;
}




- (void)awakeFromNib {
    
  

}

-(void)strFont:(NSString *)str
{
      CGSize size = [str sizeWithFont:self.MassgeLable.font constrainedToSize:CGSizeMake(self.MassgeLable.frame.size.width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    //根据计算结果重新设置UILabel的尺寸
    [self.MassgeLable setFrame:CGRectMake(10, 20, Dev_ScreenWidth-20, size.height)];
    self.MassgeLable.text = str;
}



- (void)configureCellWithAttributes:(NSDictionary *)att
{
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;

    CGFloat height = [self.class heightOfCommentWithAttributes:att withLastHeight:0 lastWidth:screenW];

    ContractView *view = [[ContractView alloc]initWithFrame:CGRectMake(0, 0, screenW,height + 15)];
    
    [self addCommentViewWith:att view:view isFirst:YES];
    [self.contentView addSubview:view];
}




+ (CGFloat)heightOfCommentWithAttributes:(NSDictionary *)att withLastHeight:(CGFloat)lastHeight lastWidth:(CGFloat)lastWidth
{
    NSString *comment = [NSString stringWithFormat:@"%@",att[@"comment"]];
    
    CGFloat height = [self.class heightWithConstraintWidth:lastWidth - 4 string:comment];
    
    
    CGFloat resultHeight = lastHeight + 40 + height;

    
    NSDictionary *queto = att[@"quote"];
    if ([queto isEqual:[NSNull null]]){
        return resultHeight;
    } else {
        return [self.class heightOfCommentWithAttributes:queto withLastHeight:resultHeight lastWidth:lastWidth - 4];
    }
    
}



+ (CGFloat)heightWithConstraintWidth:(CGFloat)width string:(NSString *)string
{
    CGSize sizeToFit = [string sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(width, CGFLOAT_MAX) lineBreakMode:NSLineBreakByWordWrapping];
    return sizeToFit.height;
}

- (void)addCommentViewWith:(NSDictionary *)att view:(ContractView *)view isFirst:(BOOL)isFirst
{
    

    view.attrib = att;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCommentView:)];
    [view addGestureRecognizer:tap];
    
    
    if (!isFirst) {
        view.backgroundColor = [UIColor yellowColor];
        view.layer.borderColor = [UIColor grayColor].CGColor;
        view.layer.borderWidth = 1;
    }
    
    NSString *content = [NSString stringWithFormat:@"%@",att[@"comment"]];
    CGFloat contentH = [self.class heightWithConstraintWidth:view.frame.size.width  string:content];
    
    CGFloat contectX = isFirst ? 30 :10;
    
    UILabel *contextLabel = [[UILabel alloc]initWithFrame:CGRectMake(contectX, view.size.height - contentH - 5, view.frame.size.width, contentH)];
    contextLabel.text = att[@"comment"];
    contextLabel.numberOfLines = 0;
    contextLabel.font = [UIFont systemFontOfSize:15];
    contextLabel.userInteractionEnabled = YES;
    
    
    CGFloat nameLabelY = isFirst ? 7:view.frame.size.height - contextLabel.frame.size.height - contentH - 8;
    
    CGFloat nameLabelX = isFirst ? 70:2;
    
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(nameLabelX, nameLabelY, view.frame.size.width - 3, 17)];
    nameLabel.userInteractionEnabled = YES;
    
    nameLabel.text = att[@"user_name"];
    

    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(view.size.width - 150 - 10,nameLabel.frame.origin.y, 150, 17)];
    timeLabel.userInteractionEnabled = YES;
    timeLabel.textAlignment = NSTextAlignmentRight;
    NSString *timeString = [NSString stringWithFormat:@"%@",att[@"submit_date"]];
    
    
        //时间转换
        NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
        [dateformatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *date = [dateformatter dateFromString:timeString];
        [dateformatter setDateFormat:@"HH:mm:ss"];
        NSString *strtime = [dateformatter stringFromDate:date];
    
    timeLabel.text = strtime;
    
    
    if (isFirst) {
        UIImageView *userImgView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 0, 30, 30)];
        userImgView.image = [UIImage imageNamed:@"action_login"];
        [view addSubview:userImgView];
    }
    
    [view addSubview:nameLabel];
    [view addSubview:contextLabel];
    [view addSubview:timeLabel];
    
    NSDictionary *queto = att[@"quote"];
    if ([queto isEqual:[NSNull null]])
    {
        return;
    }
    if (queto) {
        CGFloat heghtt = [self.class heightOfCommentWithAttributes:queto withLastHeight:0 lastWidth:view.frame.size.width];

        CGFloat x = isFirst ? 30:3;
        CGFloat w = isFirst ? view.frame.size.width - 60:view.frame.size.width - 5;
        
        CGFloat subViewY = isFirst ? 37:2;
        
        ContractView *subView = [[ContractView alloc]initWithFrame:CGRectMake(x, subViewY, w,heghtt)];
        [self addCommentViewWith:queto view:subView isFirst:NO];
        [view addSubview:subView];
    }
    
    
}

- (void)tapCommentView:(UITapGestureRecognizer *)tap
{
    ContractView *view = (ContractView *) tap.view;
    
    if (self.tapBlock) {
        self.tapBlock(view.attrib);
    }
}



- (void)tapCommentViewWithBlock:(void(^)(NSDictionary *att))block;
{
    if (block) {
        self.tapBlock = block;
    }
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end


